/* eslint-disable */
export default {
    GetRequest: function () {
        var url = window.location.href; //获取url中"?"符后的字串
        var theRequest = {},
            strs;
        if (url.indexOf("?") != -1) {
            var str = url.substr(url.indexOf("?") + 1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURI((strs[i].split("=")[1]));
            }
        }
        return theRequest;
    },
    stripscript:function(s) {
        var pattern = new RegExp("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’\"。，、？\\\\]");
        var rs = "";
        for (var i = 0; i < s.length; i++) {
            rs = rs+s.substr(i, 1).replace(pattern, '');
        }
        return rs;
    },
    setCookie: function (name, value, timers) {
        let strsec = (timers && this.getsec(timers)) || '';
        let exdate = new Date();
        exdate.setDate(exdate.getDate() + strsec * 1);
        if (value && value.constructor === Object) {
            value = JSON.stringify(value)
        }
        document.cookie = name + "=" + escape(value) + ";path=/" + ((!timers) ? "" : ";expires=" + exdate.toGMTString())
    },
    getCookies: function (name) {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"),
            regs = /^(\[|\{)[\s\S]*(\]|\})$/g;
        if (arr = document.cookie.match(reg)) {
            if (regs.test(unescape(arr[2]))) {
                return JSON.parse(unescape(arr[2]))
            }
            return unescape(arr[2]);
        } else
            return null;
    },
    getsec: function (str) { //处理cookie保存时间,默认毫秒
        var timers = str.replace(/s|d|h/g, '')
        var timersType = str.replace(/\d/g, '')
        if (timersType == "s") {
            return timers * 1000;
        } else if (timersType == "h") {
            return timers * 60 * 60 * 1000;
        } else if (timersType == "d") {
            return timers * 24 * 60 * 60 * 1000;
        } else {
            return timers;
        }
    },
    delCookie: function (name) { //删除cookie
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        if (!name) {
            var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
            if (keys) {
                for (var i = keys.length; i--;) {
                    document.cookie = keys[i] + '=;  path=/;expires=' + 'Thu, 01 Jan 1970 00:00:00 GMT'
                }
            }
            return
        }
        var cval = this.getCookies(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        }
    },
    //去除首尾的全角半角空格
    strTrim: function (value) {
        if (!value) {
            return
        }
        let v = (value + '').replace(/\s/g, "");
        return v.replace(/　/g, "");
    },
    /**
     * 计算
     */
    jisuan: function (type, ...arg) {
        let th = this;
        /* let type = arguments[0];
        let arg = arguments; */
        switch (type) {
            case '+':
                return th.xiangjia(arg);
            case '-':
                return th.xiangjian(arg);
            case '*':
                return th.chengfa(arg);
            case '/':
                return th.chufa(arg);
        }
    },
    quzheng: function (param) {
        if (/e\+/.test(param)) {
            alert('数字太大无法计算')
            return
        }

        param = parseFloat(param)

        if (/(-\d+|\d+)\.(\d*)/.exec(param)) {
            return [parseInt(RegExp['$1'] + RegExp['$2']), RegExp['$2'].length]
        }

        return [parseInt(param), 0]
    },
    //加法，减法，除法，计算比较麻烦，需要取到小数位数最大的值
    xiangjia: function (list) {
        const th = this
        let result = 0
        let maxPow = 0
        let arr
        for (let i = list.length - 1; i >= 0; i--) {
            arr = th.quzheng(list[i])
            if (arr[1] >= maxPow) {
                result = result * Math.pow(10, arr[1] - maxPow) + arr[0]
                maxPow = arr[1]
            } else {
                result += arr[0] * Math.pow(10, maxPow - arr[1])
            }
        }
        return result / Math.pow(10, maxPow)
    },
    // 减法
    // 获取小数长度最长的那个数的小数长度,
    // 把所有待计算数据乘以 Math.pow(10, 最长小数长度), 统一战线 
    // 0.1 - 0.01  ==>  10 - 1 
    // 进行减运算, 完成后除以 Math.pow(10, 最长小数长度)
    xiangjian: function (list) {
        const th = this
        let arr = th.quzheng(list[0])
        let result = arr[0]
        let maxPow = arr[1]
        for (let i = 1, len = list.length; i < len; i++) {
            arr = th.quzheng(list[i])
            if (arr[1] >= maxPow) {
                result = result * Math.pow(10, arr[1] - maxPow) - arr[0]
                maxPow = arr[1]
            } else {
                result -= arr[0] * Math.pow(10, maxPow - arr[1])
            }
        }
        return result / Math.pow(10, maxPow)
    },
    //乘法
    chengfa: function (args) {
        let th = this,
            result = 1,
            pow = 0,
            arg;
        for (let i = 0, argLen = args.length; i < argLen; i++) {
            arg = th.quzheng(args[i])
            result *= arg[0]
            pow += arg[1]
        }
        return result / Math.pow(10, pow)
    },
    //除法
    chufa: function (list) {
        const th = this
        let arr = th.quzheng(list[0])
        let result = arr[0]
        let maxPow = arr[1]
        for (let i = 1, len = list.length; i < len; i++) {
            arr = th.quzheng(list[i])
            if (arr[1] >= maxPow) {
                result = result * Math.pow(10, arr[1] - maxPow) / arr[0]
                maxPow = arr[1]
            } else {
                result /= arr[0] * Math.pow(10, maxPow - arr[1])
            }
        }
        return result
    },
    /**
     * @func 将数字转换为逗号千分位分隔
     * @param number <String> 数字
     * @return <String>
     */
    formatNumber: function (number) {
        var integer = parseInt(number);
        if (!isNaN(integer) && integer && (number = number.toString())) {
            var dot = number.indexOf('.');
            var decimal = '';
            if (dot > 0) {
                decimal = number.substr(dot);
            }
            return integer.toLocaleString().replace(/\.\d+$/, '') + decimal
        }
        return number
    },
    /**
     * @func 将数字转换为中文大写
     * @param number <String> 数字
     * @return <String>
     */
    numberToCN: function (number) {
        //汉字的数字
        var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
        //基本单位
        var cnIntRadice = new Array('', '拾', '佰', '仟');
        //对应整数部分扩展单位
        var cnIntUnits = new Array('', '万', '亿', '兆');
        //对应小数部分单位
        var cnDecUnits = new Array('角', '分', '毫', '厘');
        //整数金额时后面跟的字符
        var cnInteger = '整';
        //整型完以后的单位
        var cnIntLast = '元';
        //最大处理的数字
        var maxNum = 999999999999999.9999;
        //金额整数部分
        var integerNum;
        //金额小数部分
        var decimalNum;
        //输出的中文金额字符串
        var chineseStr = '';
        //分离金额后用的数组，预定义
        var parts;
        if (number == '') {
            return '';
        }
        var isMinus = number < 0;
        number = Math.abs(parseFloat(number));
        if (number >= maxNum) {
            //超出最大处理数字
            return '';
        }
        if (number == 0) {
            chineseStr = cnNums[0] + cnIntLast + cnInteger;
            return chineseStr;
        }
        //转换为字符串
        number = number.toString();
        if (number.indexOf('.') == -1) {
            integerNum = number;
            decimalNum = '';
        } else {
            parts = number.split('.');
            integerNum = parts[0];
            decimalNum = parts[1].substr(0, 4);
        }
        //获取整型部分转换
        if (parseInt(integerNum, 10) > 0) {
            var zeroCount = 0;
            var IntLen = integerNum.length;
            for (var i = 0; i < IntLen; i++) {
                var n = integerNum.substr(i, 1);
                var p = IntLen - i - 1;
                var q = p / 4;
                var m = p % 4;
                if (n == '0') {
                    zeroCount++;
                } else {
                    if (zeroCount > 0) {
                        chineseStr += cnNums[0];
                    }
                    //归零
                    zeroCount = 0;
                    chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                }
                if (m == 0 && zeroCount < 4) {
                    chineseStr += cnIntUnits[q];
                }
            }
            chineseStr += cnIntLast;
        }
        //小数部分
        if (decimalNum != '') {
            var decLen = decimalNum.length;
            for (var i = 0; i < decLen; i++) {
                var n = decimalNum.substr(i, 1);
                if (n != '0') {
                    chineseStr += cnNums[Number(n)] + cnDecUnits[i];
                }
            }
        }
        if (chineseStr == '') {
            chineseStr += cnNums[0] + cnIntLast + cnInteger;
        } else if (decimalNum == '') {
            chineseStr += cnInteger;
        }
        if (isMinus) {
            chineseStr = '负' + chineseStr
        }
        return chineseStr;
    },
    toFixed: function (target, length) {
        target = String(target)
        const match = target.match(/^\-?(\d*)(\.(\d*))?$/)
        if (!match) {
            return
        }

        let x = match[3]


        function zero(i) {
            let str = ''
            while (i--) {
                str += '0'
            }
            return str
        }
        // 可以用正则匹配一下
        if (x) {
            if (x.length > length) {
                let v = x.charAt(length),
                    prev_v = x.charAt(length - 1)
                x = x.slice(0, length - 1) + '' + (v >= 5 ? parseInt(prev_v) + 1 : prev_v)
            } else {
                x = (x + zero(length - x.length))
            }
        }

        return match[1] + '.' + (x || zero(length))
    },
    supportCss3: function (style) {
        var prefix = ['webkit', 'Moz', 'ms', 'o'],
            i, humpString = [],
            htmlStyle = document.documentElement.style,
            _toHumb = function (string) {
                return string.replace(/-(\w)/g, function ($0, $1) {
                    return $1.toUpperCase();
                });
            };
        for (i in prefix)
            humpString.push(_toHumb(prefix[i] + '-' + style));
        humpString.push(_toHumb(style));
        for (i in humpString)
            if (humpString[i] in htmlStyle) return true;
        return false;
    },
}
/* eslint-enable */